package com.kksoft.readcm.objects;

import android.os.Build;
import android.util.Log;

import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.activity.App;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/*
 * import android.os.Build;
import com.ideashower.readitlater.a.af;
import com.ideashower.readitlater.activity.App;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.a.a.b;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
 */

public final class w
{
  private int a;
  private final List<BasicNameValuePair> mList;  //old b
  private DefaultHttpClient httpClient;   //old c
  private r d;
  private int loginResult;
  private int httpStatusCode;
  private ErrorReport g;

  public w(Appconfig paramaf, int logintype, boolean isStoreUP, DefaultHttpClient defaultHttpClient)
  {
    this.a = logintype;
    this.httpClient = defaultHttpClient;
    this.mList = new ArrayList<BasicNameValuePair>();
    if (this.a != 3)
    {
    	addParam(new BasicNameValuePair("apikey", "b29daj86Tb6f1qcQ57A79f0V38gPI514"));//App.e().b()));
    }
    if ((this.a == 1) && (this.a == 2) && (this.a == 4))
    {
    	addParam(new BasicNameValuePair("format", "json"));
    }
    if (isStoreUP)
    {
      this.mList.add(new BasicNameValuePair("username", paramaf.getLoginName()));
      this.mList.add(new BasicNameValuePair("password", paramaf.getLoginPass()));
    }
  }

  public final void httpRequest()
  {
	  int i = 0;
	  String strUrl;
	  switch (a)
	  {
	  case 1:
		  strUrl = "https://readitlaterlist.com/v2/get";
		  break;
	  case 2:
		  strUrl = "https://readitlaterlist.com/v2/send";
		  break;
	  case 3:
		  strUrl = "http://text.readitlaterlist.com/v3beta/iphone";
		  break;
	  case 4:
		  strUrl = "https://readitlaterlist.com/v2/search";
		  break;
	  case 5:
		  strUrl = "https://readitlaterlist.com/v2/shared";
		  break;
	  case 6:
		  strUrl = "https://readitlaterlist.com/v2/auth";
		  break;
	  case 7:
		  strUrl = "https://readitlaterlist.com/v2/signup";
		  break;
	  case 8:
		  strUrl = "https://readitlaterlist.com/v2/meta";
		  break;
	  case 9:
		  strUrl = "https://readitlaterlist.com/v2/listinfoupdate";
		  break;
	  default:
		  strUrl = null;
		  break;
	  }
	  HttpPost httppost= new HttpPost(strUrl);
      StringBuilder stringbuilder = new StringBuilder("RIL; ");
      stringbuilder.append(App.e().e()).append("; ");
      stringbuilder.append(m.f()).append("; ").append("Android ");
      stringbuilder.append(android.os.Build.VERSION.SDK_INT).append("; ");
      stringbuilder.append(Build.MANUFACTURER).append("; ");
      stringbuilder.append(Build.MODEL).toString();
      httppost.setHeader("User-Agent", stringbuilder.toString());
      try
      {
    	  httppost.setEntity(new UrlEncodedFormEntity(mList, "UTF-8"));
      if(i == 0)
      {
    	  HttpResponse httpResponse = httpClient.execute(httppost);
		
    	  HttpEntity httpentity = httpResponse.getEntity();
          this.httpStatusCode = httpResponse.getStatusLine().getStatusCode();
          if(httpentity == null)
          {
        	  ClientConnectionManager clientconnectionmanager = httpClient.getConnectionManager();
              TimeUnit timeunit = TimeUnit.SECONDS;
              clientconnectionmanager.closeIdleConnections(5L, timeunit);
          }
          else
          {
        	  if(httpStatusCode == 200) 
        	  {
        		  this.loginResult=1;
        		  if(d == null) 
        		  {
						httpentity.consumeContent();
        		  }
        		  else
        		  {
        			  InputStream inputstream = null;
					try {
						inputstream = httpentity.getContent();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        		        int l = d.a(inputstream);
        		        try {
							inputstream.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
        		  }
        	  }
          }
      }
      //else
      {
    	  if(i == 0)
              i = 1;
          else
          if(i != 3 && g == null)
          {
              //String s11 = App.a(0x7f070072);
              //ErrorReport errorreport11 = new ErrorReport(s11);
              //g = errorreport11;
          }
    	  this.loginResult = i;
          d = null;
          httpClient = null;
          mList.clear(); 
      }
      }
      catch (UnsupportedEncodingException e) {
  		// TODO Auto-generated catch block
  		e.printStackTrace();
  	}
      catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
      catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
  }

  public final void a(r paramr)
  {
    this.d = paramr;
  }

  public final void addParam(BasicNameValuePair paramBasicNameValuePair)
  {
    this.mList.add(paramBasicNameValuePair);
  }

  public final int getLoginResult()
  {
    return this.loginResult;
  }

  public final int c()
  {
    return this.httpStatusCode;
  }

  public final ErrorReport d()
  {
    return this.g;
  }
}